package cn.neusoft.yp.controller;

import cn.neusoft.yp.mapper.ProdMapper;
import cn.neusoft.yp.pojo.Page;
import cn.neusoft.yp.pojo.Product;
import cn.neusoft.yp.service.ProdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * Created by gaosheng on 2017/4/27.
 */
@Controller
public class ProductController {

    @Autowired
    private ProdMapper prodMapper ;

    @Autowired
    private ProdService prodService ;


    @RequestMapping("prodList1")
    public String findAll(Model model,int thispage, int rowperpage,@RequestParam(value="name",required=false) String name,
                          @RequestParam(value="category",required=false) String category,@RequestParam(value="minprice",required=false) Double minprice,@RequestParam(value="maxprice",required=false) Double maxprice){
        //thispage,rowperpage 一定不会为空
        //4个查询条件
        if(name==null){
            name = "" ;
        }
        if(category==null){
            category = "" ;
        }
        if(minprice==null||"".equals(minprice)){
            minprice = Double.MIN_VALUE ;
        }
        if(maxprice==null||"".equals(maxprice)){
            maxprice = Double.MAX_VALUE ;
        }
        Page<Product> page = prodService.findAll(thispage,rowperpage,name,category,minprice,maxprice) ;
        model.addAttribute("page", page) ;
        return "prod_list1";

    }

    @RequestMapping("prodList")
    public String findAll(Model model, int thispage, int rowperpage){
        Page page = prodService.toPage(thispage,rowperpage) ;
        int prepage = page.getPrepage() ;
        int nextpage = page.getNextpage() ;
        List<Product> prodList = prodService.findProd(prepage,nextpage) ;

        model.addAttribute("prodList",prodList) ;
        model.addAttribute("page", page) ;

        return "prod_list";
    }

    @RequestMapping("prod")
    public String findOne(Model model,String id){

        Product prodOne = prodService.findOne(id) ;
        model.addAttribute("prodOne",prodOne) ;
        return "prod_info" ;
    }

    @RequestMapping("updatePnum")
    public String updatePnum(String pid,int newPnum){
        prodMapper.updatePnum(pid,newPnum);
        return "redirect:backProdList" ;
    }


}
